//
//  NSString+Extend.m
//  CFFinanceProject
//
//  Created by chunfeng on 2018/4/9.
//  Copyright © 2018年 xiaowen. All rights reserved.
//

#import "NSString+Extend.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Extend)


+ (BOOL)stringIsNullOrEmpty:(NSString *)str
{
    return (str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0);
}

- (BOOL)isNull{
    return (self == nil || [self isKindOfClass:[NSNull class]]);
}

- (BOOL)isNullOrEmpty{
    return (self == nil || [self isKindOfClass:[NSNull class]] || self.length == 0);
    
}

+(NSString *)getStringIsNotNull:(NSString *)str{
    if(str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0){
        return @"";
    }else{
        return str;
    }
}
- (NSString *)removeNullOrEmptyByDefaultString:(NSString *)defaultString{
    BOOL isEmpty = [[self class] stringIsNullOrEmpty:self];
    if (isEmpty) {
        return defaultString;
    }else{
        return self;
    }
}

/**
 手机号隐藏
 
 @param phoneNumber   手机号码
 @return 返回隐藏后的手机号
 */
+ (NSString *)stringByReplacingPhoneNumber:(NSString *)phoneNumber{
    NSString *numberString = [phoneNumber stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    return numberString;
}


/**
 银行卡号隐藏
 
 @param cardNumber 银行卡号
 @return 返回隐藏后的银行卡号
 */
+ (NSString *)stringByReplacingBankCardNumber:(NSString *)cardNumber{
    NSString *numberString = cardNumber;
    if(cardNumber.length > 12){
       numberString  = [cardNumber stringByReplacingCharactersInRange:NSMakeRange(2, 12) withString:@"**** **** **** "];
    }
    return numberString;
    
}



/**
 截取字符串方法封装

 @param startString 开始字符
 @param endString 结束字符
 @return 截取完毕的字符
 */
- (NSString*)subStringFrom:(NSString *)startString to:(NSString *)endString{
    
    NSRange startRange = [self rangeOfString:startString];
    
    NSRange endRange = [self rangeOfString:endString];
    
    NSRange range = NSMakeRange(startRange.location + startRange.length,
                                endRange.location - startRange.location - startRange.length);
    
    return [self substringWithRange:range];
    
}

- (NSString *)md5{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}


@end
